import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import homeRoute from "./model/home";
import Layout from "@/layout/index.vue";

const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/home",
    name: "layout",
    redirect: "/homePage",
    component: Layout,
    children: [...homeRoute],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  if (to.path !== "/login" && !token) {
    next("/login");
  } else {
    next();
  }
});

export default router;
